package com.shiyi.system.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shiyi.core.utils.result.Result;
import com.shiyi.core.vo.Role;
import com.shiyi.system.annotation.OperaLog;
import com.shiyi.system.entity.SysRole;
import com.shiyi.system.service.SysRoleService;
import com.shiyi.system.utils.EntityConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shiyi
 * @since 2020-11-19
 */
@Api(value = "角色管理", tags = "角色管理")
@RestController
@RequestMapping("/system/role")
public class SysRoleController {


    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 根据userId查询角色信息
     */
    @ApiOperation("根据userId查询角色信息")
    @GetMapping("/queryRolesByUserId")
    public Result<List<Role>> queryRolesByUserId(@RequestParam("userId") Integer userId) {
       List<Role> roles = sysRoleService.queryRolesByUserId(userId);
       return roles.size() > 0 ? Result.success(roles) : Result.fail();
    }


    /**
     * 查询所有角色信息
     */
    @ApiOperation("查询所有角色信息")
    @GetMapping("/listRole")
    public Result<PageInfo<SysRole>> listRole(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                   @RequestParam(value = "keyword", required = false) String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> sysRoles = sysRoleService.listRole(keyword);
        PageInfo<SysRole> rolePageInfo = new PageInfo<>(sysRoles);
        return Result.success(rolePageInfo);
    }


    /**
     * 新增角色信息
     */
    @OperaLog(operaModule = "角色管理", type = 1)
    @ApiOperation("新增角色信息")
    @PostMapping("/insert")
    public Result<Object> insert(@RequestBody @Valid SysRole role, BindingResult res) {
        if (res.hasErrors()) {
            return Result.BindingError(res);
        }
        if (!sysRoleService.getRoleByCode(role.getRoleCode())) {
            return Result.fail(EntityConstant.EXIST_ROLE_CODE);
        }
        return sysRoleService.insertRole(role) ? Result.success() : Result.fail();
    }

    /**
     * 修改角色
     */
    @OperaLog(operaModule = "角色管理", type = 2)
    @ApiOperation("修改角色")
    @PostMapping("/update")
    public Result<Object> update(@RequestBody @Valid SysRole role, BindingResult res) {
        if (res.hasErrors()) {
            return Result.BindingError(res);
        }
        if (!sysRoleService.getRoleByCodeAndId(role.getId(), role.getRoleCode())) {
            return Result.fail(EntityConstant.EXIST_ROLE_CODE);
        }
        return sysRoleService.updateRole(role) ? Result.success() : Result.fail();
    }

    /**
     * 删除角色信息
     */
    @OperaLog(operaModule = "角色管理", type = 3)
    @ApiOperation("删除角色信息")
    @DeleteMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable Integer id) {
        return sysRoleService.deleteRole(id) ? Result.success() : Result.fail();
    }



}

